package com.gtw.flyweight;

import java.util.HashMap;

/**
 * 享元工厂,构造一个池容器,同时提供从池容器中获取对象的方法
 */
public class FlyWeightFactory {
    /**
     * 定义池容器
     */
    private static HashMap<String, FlyWeight> pool = new HashMap<String, FlyWeight>();

    public static FlyWeight getFlyWeight(String extrinsic){
        FlyWeight flyWeight = null;
        if(pool.containsKey(extrinsic)){
            flyWeight = pool.get(extrinsic);
        }else {
            flyWeight = new ConcreteFlyweight(extrinsic);
            pool.put(extrinsic, flyWeight);
        }
        return flyWeight;
    }
}
